<?php
class C_users extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_users');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			default :
				$data["s_groups"] = $this->m_users->s_groups();
		//		$data["m_staff_id"] = $this->m_users->m_staff_id();
				$this->load->view('v_users',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['user_name'] = nosql_post("user_name");
		$data['staff_name'] = nosql_post("staff_name");
		echo $this->m_users->_list($data);
	}
	
	function _insert(){
		
		$data = array(
			'user_name' => $this->input->post('user_name'), 
			's_groups_id' => $this->input->post('s_groups_id'), 
			'user_passwd' => md5($this->input->post('user_password')), 
            
            'user_active' => 'y'
        );
        
		$this->m_users->_insert($data);
		echo 1;
	}
	
	function _update(){
		$user_id = $this->input->post('user_id');
		
		$data = array(
			'user_name' => $this->input->post('user_name'), 
			's_groups_id' => $this->input->post('s_groups_id')
		);
		
		$user_password = $this->input->post('user_password'); 
		
		if ($user_password != "")
			$data["user_passwd"] = md5($user_password);
			
		$this->m_users->_update($data,$user_id);
		echo 1;
 	}
	
	function _delete(){
		$user_id= $this->input->post('user_id');
		$data = array(
			'user_active' => 'n'
		);
		$this->m_users->_delete($data,$user_id);
		echo 1;
	}
}